//
//  my1ViewController.m
//  HGY
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 顾成辉. All rights reserved.
//

#import "my1ViewController.h"
#import "MenuModel.h"
#import "sqModel.h"
#import "ThirdModel.h"
#import "RequestViewController.h"
#import "DetailViewController.h"
#import "sqManager.h"
#import "sqFenLei.h"
#import "thirdSQManager.h"

@interface my1ViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain)UITableView *tableView;
@end

@implementation my1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
  
    self.dataArray = [NSMutableArray array];
    
      [self.view addSubview:self.tableView];
    
    [self actionButton];
   


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.index == 0) {
         NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
        return arr1.count;
    }
    if (self.index == 1) {
        NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
        return arr2.count;
    }
    
    if (self.index == 2) {
        NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
        return arr3.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
        }
    
    if (self.index == 0) {
         NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
        MenuModel *model = arr1[indexPath.row];
         cell.textLabel.text = model.title;
    }
    if (self.index == 1) {
         NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
        sqModel *model = arr2[indexPath.row];
        cell.textLabel.text = model.title;
    }
    if (self.index == 2) {
        NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];

        ThirdModel *model = arr3[indexPath.row];
        cell.textLabel.text = model.title;
    }
  
        return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.index == 0) {
        NSMutableArray *arr1 = [[sqManager shareManager] queryAll];

        MenuModel *model = arr1[indexPath.row];
        DetailViewController *vc = [[DetailViewController alloc] init];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];

        
    }
    if (self.index == 1) {
         NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
        sqModel *model = arr2[indexPath.row];
        RequestViewController *vc = [[RequestViewController alloc] init];
        vc.requestUrl = model.deal_h5_url;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (self.index == 2) {
        NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
        ThirdModel *model = arr3[indexPath.row];
        VideoViewController *vc = [[VideoViewController alloc] init];
        vc.thirdmodel = model;
        [self.navigationController pushViewController:vc animated:YES];

      
    }

}



// 编辑触发按钮
- (void)actionButton
{
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionButton:)];
    self.navigationItem.rightBarButtonItem = buttonItem;
}

- (void)actionButton:(UIBarButtonItem *)button
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    if (self.tableView.editing == YES) {
        self.navigationItem.rightBarButtonItem.title = @"完成";
    } else {
        self.navigationItem.rightBarButtonItem.title = @"编辑";
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 编辑删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


// 编辑的第四步
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if (self.index == 0) {
             NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
                MenuModel *model = arr1[indexPath.row];
            [[sqManager shareManager] deleteStudentWithString:model.title];
            [tableView reloadData];
            
            }
            if (self.index == 1) {
                NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];

                sqModel *model = arr2[indexPath.row];
                [[sqFenLei shareManager] deleteStudentWithString:model.title];
               [tableView reloadData];
                
            }
            if (self.index == 2) {
                 NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
                ThirdModel *model = arr3[indexPath.row];
                [[thirdSQManager shareManager] deleteStudentWithString:model.title];
                [tableView reloadData];
                
            }
       

        }
    
   }


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if (sourceIndexPath.section == destinationIndexPath.section) {
        if (self.index == 0) {
            NSMutableArray *array = [[sqManager shareManager] queryAll];
            MenuModel *model = [[MenuModel alloc] init];
            model = array[sourceIndexPath.row];
            [array removeObjectAtIndex:sourceIndexPath.row];
            [array insertObject:model atIndex:destinationIndexPath.row];
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        }
        if (self.index == 1) {
            NSMutableArray *array = [[sqFenLei shareManager] queryAll];
            sqModel *model = [[sqModel alloc] init];
            model = array[sourceIndexPath.row];
            [array removeObjectAtIndex:sourceIndexPath.row];
            [array insertObject:model atIndex:destinationIndexPath.row];
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        }
        
        if (self.index == 2) {
            NSMutableArray *array = [[thirdSQManager shareManager] queryAll];
            ThirdModel *model = [[ThirdModel alloc] init];
            model = array[sourceIndexPath.row];
            [array removeObjectAtIndex:sourceIndexPath.row];
            [array insertObject:model atIndex:destinationIndexPath.row];
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        }
      
    }
}


- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
